package com.darthsith.bolloauto.util;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import com.darthsith.bolloauto.model.InfoBollo;
import com.darthsith.bolloauto.model.Pagamento;
import com.darthsith.bolloauto.model.Veicolo;

public class ResolveService {
		
	private String targa;
	private String categoria;
	
	public ResolveService(String targa) {
		this.targa=targa;
	}
	
	public ResolveService(String targa, String categoria) {
		this.targa=targa;
		this.categoria=categoria;
	}
	
	private InputStream readStream() {
		InputStream inputStream=null;
		if (categoria==null)
			categoria=Constants.VALUE_CATEGORIA_AUTO;
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(Constants.ADDRESS_URL);
		List <NameValuePair> parameters = new ArrayList <NameValuePair>(); 
        parameters.add(new BasicNameValuePair(Constants.PARAM_CATEGORIA, categoria)); 
        parameters.add(new BasicNameValuePair(Constants.PARAM_TARGA, targa)); 
        parameters.add(new BasicNameValuePair(Constants.PARAM_TIPO_SERVIZIO, Constants.VALUE_TIPO_SERVIZIO)); 
        parameters.add(new BasicNameValuePair(Constants.PARAM_SUBMIT, Constants.VALUE_SUBMIT)); 
        try {
	        UrlEncodedFormEntity sendentity = new UrlEncodedFormEntity(parameters, HTTP.UTF_8);
			post.setEntity(sendentity);
			HttpResponse response=client.execute(post);
			HttpEntity entity=response.getEntity();
			inputStream=entity.getContent();
        }
        catch (Exception e) {
        	throw new RuntimeException("Error connecting to service");
		}
        return inputStream;
	}
	
	/**
	 * <p>Parsa la pagina html per estrarre tutti i dati di interesse del veicolo
	 * e del pagamento.</p>
	 * <p>L'oggetto restituito è di tipo InfoBollo, che contiene le entity di Veicolo
	 * e di Pagamento</p>
	 * 
	 * @return
	 */
	public InfoBollo getInfoBollo() {
		Scanner scanner=new Scanner(readStream());
		Map<String, String> map=new HashMap<String, String>();
		while (scanner.hasNext()) {
			String line=scanner.nextLine();
			String desc="";
			String val="";
			if (line.contains("out3")) {
				line=line.substring(line.indexOf("out3")+6);
				desc=line.substring(0, line.indexOf("<")-1);
				line=line.substring(line.indexOf("out4")+6);
				val=line.substring(0, line.indexOf("<"));
				map.put(desc, val);
			}
			if (line.contains("impsin")) {
				line=line.substring(line.indexOf("impsin")+8);
				desc=line.substring(0, line.indexOf("<")-1);
				line=scanner.nextLine();
				if (line.replace(" ", "").equals(""))
					line=scanner.nextLine();
				line=line.substring(line.indexOf("impdx")+7);
				val=line.substring(0, line.indexOf("<"));
				map.put(desc, val);
			}
			if (line.contains("totsin")) {
				line=line.substring(line.indexOf("totsin")+8);
				desc=line.substring(0, line.indexOf("<")-1);
				line=scanner.nextLine();
				line=line.substring(line.indexOf("totdx")+7);
				val=line.substring(0, line.indexOf("<"));
				map.put(desc, val);
			}
			if (line.contains("scadsin")) {
				line=line.substring(line.indexOf("scadsin")+9);
				desc=line.substring(0, line.indexOf("<")-1);
				line=scanner.nextLine();
				line=line.substring(line.indexOf("scaddx")+8);
				val=line.substring(0, line.indexOf("<"));
				map.put(desc, val);
			}
		}
		return new InfoBollo(new Veicolo(map), new Pagamento(map));
	}

}
